package cn.gzsf.csmall.passport.controller;

import cn.gzsf.csmall.passport.pojo.param.AdminAddNewParam;
import cn.gzsf.csmall.passport.pojo.param.AdminLoginInfoParam;
import cn.gzsf.csmall.passport.security.AdminDetails;
import cn.gzsf.csmall.passport.service.IAdminService;
import cn.gzsf.csmall.passport.web.JsonResult;
import cn.gzsf.csmall.passport.web.ServiceCode;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/admin")
@Api(tags = "01.管理员管理模块")
@Validated
public class AdminController {

    @Autowired
    private IAdminService adminService;

    //http://localhost:9181/admin/add-new
    @PreAuthorize("hasAnyAuthority('/ams/admin/add-new')")
    @PostMapping("/add-new")
    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid AdminAddNewParam adminAddNewParam)  {
        log.debug("开始处理[添加管理员]的请求，参数{}",adminAddNewParam);
        adminService.addNew(adminAddNewParam);
        log.debug("处理[添加管理员]的请求，完成!");
        return JsonResult.ok();
    }
    @PostMapping("/login")
    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 50)
    public JsonResult login(AdminLoginInfoParam adminLoginInfoParam)  {
        log.debug("开始处理[管理员登录]的请求，参数{}",adminLoginInfoParam);
        adminService.login(adminLoginInfoParam);
        return JsonResult.ok();
    }

    @PostMapping("/list")
    @PreAuthorize("hasAnyAuthority('/ams/admin/read')")
    @ApiOperation("查询管理员列表")
    @ApiOperationSupport(order = 420)
    public JsonResult list(@ApiIgnore  @AuthenticationPrincipal AdminDetails user)  {
        log.debug("开始处理[管理员列表]的请求，当事人ID:{}",user.getId());
        return JsonResult.fail(ServiceCode.ERR_UNKNOWN,"此功能尚未开发");
    }




}
